package com.markbuikema.juliana32.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.markbuikema.juliana32.enums.Category;
import com.markbuikema.juliana32.enums.CompType;
import com.markbuikema.juliana32.enums.Warning;
import com.markbuikema.juliana32.model.teams.GameEvent;
import com.markbuikema.juliana32.model.teams.League;
import com.markbuikema.juliana32.model.teams.Table;
import com.markbuikema.juliana32.model.teams.TableRow;
import com.markbuikema.juliana32.model.teams.Team;

public class KnvbParser {

	public static List<Team> parseTeams(String json) {
		List<Team> teams = new ArrayList<Team>();
		try {
			JSONObject root = new JSONObject(json);
			JSONArray list = root.getJSONArray("List");
			for (int t = 0; t < list.length(); t++) {
				JSONObject teamJson = list.getJSONObject(t);
				String id = teamJson.optString("teamid");
				String name = teamJson.optString("teamname");
				String catString = teamJson.optString("categorie");

				Team team = new Team(id, name, Category.fromName(catString));
				teams.add(team);
			}
		} catch (JSONException e) {
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		return teams;
	}

	public static List<GameEvent> parseGames(String json) {
		List<GameEvent> games = new ArrayList<GameEvent>();

		try {
			JSONObject root = new JSONObject(json);
			JSONArray list = root.getJSONArray("List");
			for (int g = 0; g < list.length(); g++) {
				JSONObject gameJson = list.getJSONObject(g);
				String id = gameJson.optString("MatchId");
				String date = gameJson.optString("Datum");
				String time = gameJson.optString("Tijd");
				String thuisLogo = gameJson.optString("ThuisLogo");
				String thuisTeam = gameJson.optString("ThuisClub");
				String thuisTeamNumber = gameJson.optString("ThuisClubNummer");
				String thuisTeamId = gameJson.optString("ThuisTeamId");
				String thuisGoals = gameJson.optString("PuntenTeam1");
				String uitLogo = gameJson.optString("UitLogo");
				String uitTeam = gameJson.optString("UitClub");
				String uitClubNumber = gameJson.optString("UitClubNummer");
				String uitTeamId = gameJson.optString("UitTeamId");
				String uitGoals = gameJson.optString("PuntenTeam2");
				String referee = gameJson.optString("Scheidsrechter");
				String assistantReferee1 = gameJson.optString("Hulpscheidsrechter1");
				String assistantReferee2 = gameJson.optString("Hulpscheidsrechter2");
				String locationId = gameJson.optString("Facility_Id");
				String locationName = gameJson.optString("Facility_naam");
				String locationCity = gameJson.optString("Facility_Stad");
				String locationZip = gameJson.optString("Facility_Postcode");
				String locationAddress = gameJson.optString("Facility_Adres");
				String gameNumber = gameJson.optString("WedstrijdNummer");
				String compId = gameJson.optString("CompId");
				String compType = gameJson.optString("CompType");
				String bijz = gameJson.optString("Bijzonderheden");
				Warning warning;
				try {
					warning = Warning.valueOf(bijz);
				} catch (Exception e) {
					warning = null;
				}

				long dateMs = DateTimeUtils.parseKnvbDate(date, time);

				GameEvent game = new GameEvent(id, dateMs, thuisLogo, thuisTeam, thuisTeamNumber, thuisTeamId, thuisGoals, uitLogo, uitTeam, uitClubNumber, uitTeamId, uitGoals, referee,
						assistantReferee1, assistantReferee2, locationId, locationName, locationCity, locationZip, locationAddress, gameNumber, compId, compType, warning);
				games.add(game);

			}

		} catch (JSONException e) {
		}

		return games;
	}

	public static List<Table> parseTables(String json) {

		try {

			JSONObject o = new JSONObject(json);

			int errorCode = o.getInt("errorcode");
			if (errorCode != 1000)
				return new ArrayList<Table>();

			JSONArray list = o.getJSONArray("List");

			Map<String, List<TableRow>> rows = new HashMap<String, List<TableRow>>();

			for (int i = 0; i < list.length(); i++) {
				JSONObject t = list.getJSONObject(i);
				String name = t.optString("naam");
				String teamId = t.optString("TeamID");
				String imgUrl = t.optString("logo");
				String position = t.optString("Positie");
				String played = t.optString("Gespeeld");
				String won = t.optString("Gewonnen");
				String draw = t.optString("Gelijk");
				String lost = t.optString("Verloren");
				String points = t.optString("Punten");
				String scored = t.optString("DoelpuntenVoor");
				String conceded = t.optString("DoelpuntenTegen");
				String minusPoints = t.optString("PuntenMindering");
				String compType = t.optString("CompType");
				String periode = t.optString("Periode");

				TableRow row = new TableRow(name, teamId, position, imgUrl, played, won, draw, lost, points, minusPoints, scored, conceded, compType, periode);

				if (!rows.containsKey(periode)) {
					rows.put(periode, new ArrayList<TableRow>());
				}

				rows.get(periode).add(row);

			}

			List<Table> tables = new ArrayList<Table>();

			for (String periode : rows.keySet()) {
				List<TableRow> periodeRows = rows.get(periode);
				Table table = new Table(periodeRows, periodeRows.get(0).getCompType(), periode);
				tables.add(table);
			}

			return tables;

		} catch (JSONException e) {
		}
		return new ArrayList<Table>();
	}

	public static void parseAndAddLeagues(String json) {

		try {
			JSONObject root = new JSONObject(json);
			JSONArray list = root.getJSONArray("List");
			for (int i = 0; i < list.length(); i++) {
				JSONObject l = list.getJSONObject(i);
				String compName = l.optString("CompName");
				String compId = l.optString("CompId");
				String className = l.optString("ClassName");
				String classId = l.optString("ClassId");
				String pouleName = l.optString("PouleName");
				String pouleId = l.optString("PouleId");
				String district = l.optString("District");
				String ct = l.optString("CompType");
				CompType compType = CompType.get(ct);

				League league = new League(compName, compId, className, classId, pouleName, pouleId, district, compType);

				String teamId = l.optString("TeamId");
				String teamCode = l.optString("TeamAanduiding");

				Team team = DataManager.getInstance().findTeamById(teamId);
				team.setCode(teamCode);
				team.addLeague(league);
			}

		} catch (JSONException e) {
		}

	}
}
